/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.gui;

import it.polimi.meteocal.EJB.entities.Event;
import it.polimi.meteocal.EJB.entities.Invite;
import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.sessionBeans.EventManager;
import it.polimi.meteocal.EJB.sessionBeans.InviteManager;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Fabio
 */
@ManagedBean(name = "iBean")
@ViewScoped

public class InviteBean {

    @EJB
    InviteManager im;
    EventManager em;

    private Invite newInvite;
    private List<String> resultList;
    private List<String> guestList;
    private boolean ExistsResultList;
    private boolean inv;
    private int id;
    private int accept;
    private String search;

    public InviteBean() {
    }
    
        public void setSearch(String search) {
        this.search = search;
    }

    public String getSearch() {
        return search;
    }

    public Invite getNewInvite() {
        if (newInvite == null) {
            newInvite = new Invite();
        }
        return newInvite;
    }

    public void removeFromGL(String user) {
        guestList.remove(user);
    }

    public void setGuestList(List<String> data) {
        this.guestList = data;
    }

    public List<String> getGuestList() {
        return guestList;
    }

    public String setResultList(List<String> result, String search) {
        this.resultList = result;
        if (this.resultList.size() > 0) {
            ExistsResultList = true;
        } else {
            ExistsResultList = false;
        }
        return "/user/invitationresults?faces-redirect=true&src=" + search;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setAccept(int accept) {
        this.accept = accept;
    }

    public int getAccept() {
        return accept;
    }

    public boolean resultFound() {
        return ExistsResultList;
    }

    public boolean noResultFound() {
        return !ExistsResultList;
    }

    public List<String> getResultList() {
        return resultList;
    }

    public String createInvite(String s, int id, String search) {
        im.create(s, id);
        return "/user/invitationresults?faces-redirect=true&id=" + id + "&src=" + search;
    }
    
        public String deleteInvite(User s, Event id, String search) {
        im.delete(s, id);
        return "/user/invitationresults?faces-redirect=true&id=" + id.getId() + "&src=" + search;
    }
        
        public boolean ceuninvito(User u, Event e){
            return !im.checkInvites(u.getUsername(), e.getId()).isEmpty();
        }
        
        public boolean nonceuninvito(User u, Event e){
            return im.checkInvites(u.getUsername(), e.getId()).isEmpty();
        }

    void uInv(String a, int id) {
        Event e = em.find(id);
        for (int i = 0; i < e.getInviteList().size(); i++) {
            if (e.getInviteList().get(i).getGuest().getUsername().equals(a)) {
                inv = true;
                i = e.getInviteList().size();
            } else {
                inv = false;
            }
        }
    }

    public boolean uIsInv(String a, int id) {
        uInv(a, id);
        return inv;
    }

    public boolean uNotInv(String a, int id) {
        uInv(a, id);
        return !inv;
    }

    public Invite find(int id) {
        return im.find(id);
    }

    public String confirmInv(User u, Event e, int b) {
        im.confirm(u, e, b);
        return "/user/event?faces-redirect=true&id=" + e.getId();
    }


}
